// pagesDoctor/addFeedback/index.js
const app = getApp()
let {
  baseUrl,
  commonUploadDoctor,
  commonUpload
} = app.globalData
import {
  ApiAddQualification,
  ApiMySkill,
  ApiSkillApply
} from '../api/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    skillArr: [],
    ops: {
      skill: "",
    },
    baseUrl,
    commonUploadDoctor,
    commonUpload,
    fileList: [],
    fileListCopy: [],

    autosize: {
      maxHeight: 500,
      minHeight: 200
    },
  },
  onLoad() {
    this.getMySkill()
  },
  getMySkill() {
    ApiMySkill().then(res => {
      let val = []
      res.data.forEach(item => {
        if (item.checked == '0') {
          val.push(item)
        }
      })
      this.setData({
        skillArr: val
      })
    })
  },
  onChange(e) {
    console.log(e)
    const type = e.currentTarget.dataset.v
    const value = e.detail
    this.setData({
      [`ops.${type}`]: value
    })

  },
  afterRead: function (event) {
    console.log(event.file)
    let that = this
    let json = JSON
    const {
      file,
    } = event.detail
    this.setData({
      type: file.type
    })

    wx.showLoading({
      title: '上传中',
      mask: true
    })
    wx.uploadFile({
      url: baseUrl + commonUploadDoctor,
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + wx.getStorageSync('token')
      },
      success(res) {
        let baseUrlLink = baseUrl + commonUpload + json.parse(res.data).data.path
        that.data.fileList.push({
          url: baseUrlLink,
        })
        that.data.fileListCopy.push(
          json.parse(res.data).data.path
        )
        that.setData({
          'fileList': that.data.fileList,
          'ops.file': that.data.fileListCopy
        })
        wx.showToast({
          title: '上传文件成功',
        })
      }
    })
  },

  //删除图片
  handleDelete(event) {
    this.data.fileList.splice(event.detail.index, 1)
    this.data.ops.pic.splice(event.detail.index, 1)
    this.setData({
      fileList: this.data.fileList,
      "ops.pic": this.data.ops.pic
    })
  },




  //保存反馈
  addFeedback() {
    ApiSkillApply({
      ...this.data.ops,
      skill: this.data.ops.skill.toString()
    }).then(res => {
      if (res.code == '200') {
        wx.showToast({
          title: "添加成功",
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000);

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})